﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing.Imaging;
using TDS.Captcha;

public class Handler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        CaptchaImage ci = null;
        // Create a CAPTCHA image using the text stored in the Session object.
        if (context.Session[GlobalSettings.CaptchaImageText] == null)
        {
            string _rdmNumber = GlobalSettings.GenerateRandomCode(4);
            ci = new CaptchaImage(_rdmNumber, 200, 50, "Century Schoolbook");
            context.Session[GlobalSettings.CaptchaImageText] = _rdmNumber;
        }
        else
        {
            ci = new CaptchaImage(context.Session[GlobalSettings.CaptchaImageText].ToString(), 200, 50, "Century Schoolbook");
        }


        // Change the response headers to output a JPEG image.
        context.Response.Clear();
        context.Response.ContentType = "image/jpeg";

        if (ci != null)
        {
            // Write the image to the response stream in JPEG format.
            ci.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg);

            // Dispose of the CAPTCHA image object.
            ci.Dispose();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}